#!/usr/bin/python3
import sys
import subprocess

def const_zero(sequence):
    return all([n == 0 for n in sequence])

def diff(sequences):
    sequence = sequences[-1]
    layer = [sequence[i] - sequence[i - 1] for i in range(1, len(sequence))]
    sequences.append(layer)
    if not const_zero(layer):
        diff(sequences)

def extend(sequences, i, reverse=False):
    if const_zero(sequences[i]):
        pos = 0 if reverse else len(sequences[i])
        sequences[i].insert(pos, 0)
    else:
        extend(sequences, i + 1, reverse)
        pos = 0 if reverse else len(sequences[i])
        value = sequences[i][0] - sequences[i + 1][0] if reverse else sequences[i][-1] + sequences[i + 1][-1]
        sequences[i].insert(pos, value)

def part12(reverse=False):
    print('part 1')
    total = 0
    for line in sys.stdin:
        sequences = [[int(n) for n in line.strip().split(' ')]]
        print(sequences)
        diff(sequences)
        print(sequences)
        extend(sequences, 0, reverse)
        print(sequences)
        value = sequences[0][0] if reverse else sequences[0][-1]
        total += value
    print(total)

if sys.argv[1] in '1':
    part12(reverse=False)
else:
    part12(reverse=True)